﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilPublic.ServerLoger;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using UtilLib;
using UtilPublic.Config;
using System.IO;
using UtilPublic.Serialization;
using System.Text;

namespace ServerBase.Config
{
    /// <summary>
    /// 配置类公用接口
    /// </summary>
    public interface IConfigBase
    {
        object GetKey();
    }

    /// <summary>
    /// 配置文件
    /// </summary>
    public static partial class Conf
    {
        /// <summary>
        /// 配置文件目录
        /// </summary>
        public static string eConfJsonPath = $"Config/JsonServer/";
        public static string eConfTextPath = @"Config/Text/";

        public static string ConfListSeparator = UtilEnum.分隔符竖线;
        public static string ConfDictionarySeparator1 = UtilEnum.分隔符竖线;
        public static string ConfDictionarySeparator2 = UtilEnum.分隔符下划线;

        /// <summary>
        /// 初始化配置
        /// </summary>
        /// <returns></returns>
        public static bool InitConf()
        {
            bool result = true;

            result = InitConfText();
            if (result)
            {
                loger.Info("读取文本配置文件成功");
            }
            else
            {
                loger.Fatal("读取文本配置文件失败");
                return result;
            }
            result = InitConfSystem();
            if (result)
            {
                loger.Info("读取系统配置文件成功");
            }
            else
            {
                loger.Fatal("读取系统配置文件失败");
                return result;
            }

            var StopwatchProcess = Stopwatch.StartNew();
            result = InitConfSettings();
            StopwatchProcess.Stop();
            loger.Info($"InitConfSettings {StopwatchProcess.Elapsed}");

            if (result)
            {
                loger.Info("读取策划配置文件成功");
            }
            else
            {
                loger.Fatal("读取策划配置文件失败");
                return result;
            }

            return result;
        }

        /// <summary>
        /// 读取Dictionary配置
        /// </summary>
        /// <param name="ConfName"></param>
        /// <returns></returns>
        public static bool ReadConfig<T1, T2>(string Name, ref Dictionary<T1, T2> confDic)
        {
            return ConfRead.ReadJsonDict(Name, ref confDic, eConfJsonPath);
        }
    }
}